package net.blank.ui.core
{
	import flash.text.StyleSheet;
	import flash.text.TextFormat;
	
	import net.blank.commonInterface.IClone;
	import net.blank.commonInterface.IDestructible;
	import net.blank.functionUtl.FunctionBinder;
	
	/**
	 * 文本皮肤
	 * @author blank
	 * 
	 */
	public class TextSkin implements IDestructible, IClone
	{
		/**
		 * 已经被摧毁
		 */
		private var _destroyed:Boolean;
		
		/**
		 * 文本格式
		 */
		private var _textFormat:TextFormat;
		
		/**
		 * CSS格式表
		 */
		private var _styleSheet:StyleSheet;
		
		/**
		 * 文本滤镜列表
		 */
		private var _filters:Array;
		
		/**
		 * 禁用交互性事件时的滤镜
		 */
		private var _disabledFilters:Array;
		
		/**
		 * 文本皮肤
		 * @param textFormat 文本格式
		 * @param styleSheet CSS格式表
		 * 
		 */
		public function TextSkin(
			textFormat:TextFormat=null,
			styleSheet:StyleSheet=null,
			disabledFilters:Array=null,
			filters:Array=null
		){
			_textFormat=textFormat;
			_styleSheet=styleSheet;
			_disabledFilters=disabledFilters;
			_filters=filters;
		}
		
		/**
		 * 文本格式(可绑定)
		 */
		public function setTextFormat(value:TextFormat):void{
			if(value != _textFormat){
				_textFormat = value;
				
				FunctionBinder.execCallbackFun(setTextFormat,[_textFormat]);
			}
		}
		/**
		 * 文本格式
		 * @return 
		 * 
		 */
		public function getTextFormat():TextFormat{
			return _textFormat;
		}
		
		/**
		 * CSS样式表(可绑定)
		 * @param value
		 * 
		 */
		public function setStyleSheet(value:StyleSheet):void{
			if(value != _styleSheet){
				_styleSheet = value;
				
				FunctionBinder.execCallbackFun(setStyleSheet,[this]);
			}
		}
		/**
		 * CSS样式表
		 * @return 
		 * 
		 */
		public function getStyleSheet():StyleSheet{
			return _styleSheet;
		}
		
		/**
		 * 禁用交互性事件时的滤镜(可绑定)
		 * @param value
		 * 
		 */
		public function setDisabledFilters(value:Array):void{
			if(value != _disabledFilters){
				_disabledFilters=value;
				
				FunctionBinder.execCallbackFun(setDisabledFilters,[this]);
			}
		}
		/**
		 * 禁用交互性事件时的滤镜
		 * @return 
		 * 
		 */
		public function getDisabledFilters():Array{
			return _disabledFilters;
		}
		
		/**
		 * 文本滤镜列表
		 * @return 
		 * 
		 */
		public function getFilters():Array{
			return _filters;
		}
		/**
		 * 文本滤镜列表
		 * @param value
		 * 
		 */
		public function setFilters(value:Array):void{
			if(value != _filters){
				_filters=value;
				
				FunctionBinder.execCallbackFun(setFilters,[this]);
			}
		}
		
		public function get destroyed():Boolean{
			return _destroyed;
		}
		
		public function destroy():void{
			_textFormat=null;
			_styleSheet.clear();
			_styleSheet=null;
			
			FunctionBinder.clearBindingFuns(
				setTextFormat,
				setStyleSheet,
				setDisabledFilters,
				setFilters
			);
			
			_destroyed=true;
		}
		
		public function clone():*{
			return new TextSkin(
				_textFormat ? cloneTextFormat(_textFormat) : null,
				_styleSheet ? cloneStyleSheet(_styleSheet) : null,
				Skin.cloneFilters(_disabledFilters),
				Skin.cloneFilters(_filters)
			);
		}
		
		/**
		 * 拷贝文本格式
		 * @param textFormat
		 * @return 
		 * 
		 */
		public static function cloneTextFormat(textFormat:TextFormat):TextFormat{
			var tf:TextFormat=new TextFormat(
				textFormat.font,
				textFormat.size,
				textFormat.color,
				textFormat.bold,
				textFormat.italic,
				textFormat.underline,
				textFormat.url,
				textFormat.target,
				textFormat.align,
				textFormat.leftMargin,
				textFormat.rightMargin,
				textFormat.indent,
				textFormat.leading
			);
			tf.blockIndent=textFormat.blockIndent;
			tf.bullet=textFormat.bullet;
			tf.kerning=textFormat.kerning;
			tf.letterSpacing=textFormat.letterSpacing;
			
			return tf;
		}
		
		/**
		 * 拷贝 CSS 样式表
		 * @param styleSheet
		 * @return 
		 * 
		 */
		public static function cloneStyleSheet(styleSheet:StyleSheet):StyleSheet{
			var newStyleSheet:StyleSheet;
			
			newStyleSheet=new StyleSheet();
			var styleNames:Array=styleSheet.styleNames;
			for each(var styleName:String in styleSheet){
				newStyleSheet.setStyle(styleName,styleSheet.getStyle(styleName));
			}
			
			return newStyleSheet;
		}
	}
}